package com.xmb.wechat.view;

import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.base.BaseFragment;
import com.xmb.wechat.delegate.TabAdapter;
import com.xmb.wechat.view.fragment.AliPayFragment;
import com.xmb.wechat.view.fragment.WeChatFragment;
import com.xmb.wechat.view.wechat.setting.WechatMyInfoActivity;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/29 15:50
 */
public class ModuleMainActivity extends BaseActivity {

    @BindView(R2.id.view_pager)
    ViewPager        mViewPager;
    @BindView(R2.id.tab_layout)
    SegmentTabLayout mTabLayout;

    private ArrayList<BaseFragment> mFragments    = new ArrayList<>();
    private String[]                mTitleDataArr = new String[]{"微信", "其他"};
    private WeChatFragment          mChatFragment;
    private AliPayFragment          mAliPayFragment;
    private int                     mCurrentPosition;

    public ModuleMainActivity() {
        super(R.layout.activity_module_main);
    }

    @Override
    protected void initView() {
        //initFragments
        mChatFragment = new WeChatFragment();
        mFragments.add(mChatFragment);
        mAliPayFragment = new AliPayFragment();
        mFragments.add(mAliPayFragment);
        //initViewPager
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mViewPager.setAdapter(new TabAdapter(getSupportFragmentManager(), mFragments, new ArrayList<>(Arrays.asList(mTitleDataArr))));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mCurrentPosition = position;
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        //initTabLayout
        mTabLayout.setTabData(mTitleDataArr);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    @OnClick(R2.id.iv_user)
    public void onViewClicked() {
        WechatMyInfoActivity.start(this,mCurrentPosition);
    }
}
